import { ITypeEntity } from '../types';

interface IDataMap {
  [propName: string]: ITypeEntity;
}

export const getTypeMap = (types: ITypeEntity[]) => {
  /**
   * 索引
   * key: _id
   */
  const map: IDataMap = {};
  const mapFk: {
    [propName: string]: string;
  } = {};
  types.forEach((item) => {
    map[item._id] = item;
    mapFk[item.fkId + '-' + item.type] = item._id;
    const index = types.findIndex((_) => _._id === item._id);
    if (index > -1) {
      types[index] = item;
    } else {
      types.push(item);
    }
  });
  return {
    map,
    mapFk,
  };
};
